package com.robot.controller;

import com.robot.dao.dto.FileDTO;
import com.robot.dao.dto.FileOverViewDTO;
import com.robot.dao.dto.IDDTO;
import com.robot.common.result.Response;
import com.robot.dao.dto.TemplateFileDTO;
import com.robot.dao.vo.FileOverViewVO;
import com.robot.service.FileService;
import com.robot.dao.vo.FileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/file")
@Slf4j
@Api(tags = "文件相关接口")
public class FileController {


    @Autowired
    private FileService fileService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")

    public Response<List<FileOverViewVO>> upload(@RequestParam("userId") String userId,
                                                 @RequestParam("knowledgeId") String knowledgeId,
                                                 @RequestParam("knowledgeName") String knowledgeName,
                                                 @RequestParam(value = "minioBucketName",required = false) String minioBucketName,
                                                 @RequestParam("files") MultipartFile[] files
    ) {
        for(MultipartFile file : files){
            if (ObjectUtils.isEmpty(file) || file.getSize() <= 0) {
                return Response.error("文件为空");
            }
        }
        if (minioBucketName==null||minioBucketName==""){
            minioBucketName="undefined";
        }
        FileDTO fileDTO = new FileDTO();
        fileDTO.setUserId(userId);
        fileDTO.setKnowledgeId(knowledgeId);
        fileDTO.setKnowledgeName(knowledgeName);
        fileDTO.setMinioBucketName(minioBucketName);
        fileDTO.setFiles(files);
        System.out.println(fileDTO);
        List<FileOverViewVO> upload;
        try {
            upload = fileService.upload(fileDTO);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Response.success(upload);
    }

    @PostMapping("/queryByFileId")
    @ApiOperation(value = "根据ID查询文件详情")
    public Response<FileVO> queryByFileId(@RequestBody IDDTO iddto){
        FileVO fileVO = fileService.queryByFileId(iddto);
        return Response.success(fileVO);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除文件（逻辑删除）")
    public Response deleteByFileId(@RequestBody IDDTO iddto){
        fileService.delete(iddto);
        return Response.success();
    }

    @PostMapping("/updateFileState")
    @ApiOperation(value = "更新文件状态")
    public Response updateFileState(@RequestBody FileOverViewDTO fileOverViewDTO){
        fileService.updateFileState(fileOverViewDTO);
        return Response.success();
    }

    @PostMapping("/downloadTemplateFile")
    @ApiOperation(value = "下载模板文件")
    public Response downloadTemplateFile(@RequestBody TemplateFileDTO templateFileDTO){
        if (templateFileDTO.getFileTemplateType()==1){//下载客服模板
            IDDTO idDTO = new IDDTO();
            idDTO.setFileId("272");
            FileVO fileVO = fileService.queryByFileId(idDTO);
            return Response.success(fileVO);
        }else if (templateFileDTO.getFileTemplateType()==2){//下载批量回答模板
            IDDTO idDTO = new IDDTO();
            idDTO.setFileId("273");
            FileVO fileVO = fileService.queryByFileId(idDTO);
            return Response.success(fileVO);
        }
        return Response.error("文件类型错误");
    }

}
